/**
 * 初始化对话框
 */
var VpnExclusiveLineDlg = {
    VpnExclusiveLineData : {}
};

/**
 * 关闭此对话框
 */
VpnExclusiveLineDlg.close = function() {
    parent.layer.close(window.parent.VpnExclusiveLine.layerIndex);
}

/**
 * 提交添加
 */
VpnExclusiveLineDlg.addSubmit = function() {
    var serverId = $("#serverId").val();
    var username = $("#username").val();
    var serviceDays = $("#serviceDays").val();

    if (serverId == null || serverId == ''){
        Feng.error("请选择专属服务器")
    }else if (username == null || username == ''){
        Feng.error("用户账号不能为空")
    }else if (serviceDays <=0 || serviceDays >365){
        Feng.error("套餐有效期1-365天")
    }else{
        var confirmIndex = layer.confirm("确定新增专属线路套餐?",
            {btn:['确认添加','取消添加']},
            function () {
                layer.closeAll()
                var loadIndex = layer.load(0,{
                    shade:false
                });

                var ajax = new $ax(Feng.ctxPath + "/vpnExclusive/add", function(data){
                    Feng.success("添加成功!");
                    window.parent.VpnExclusiveLine.refresh();
                    VpnExclusiveLineDlg.close();
                    layer.closeAll();
                },function(data){
                    Feng.error(data.responseJSON.message + "!");
                    layer.closeAll();
                });
                ajax.async = false;
                ajax.set("serverId",serverId);
                ajax.set("username",username);
                ajax.set("availableDays",serviceDays);
                ajax.start();
            },function () {
                layer.msg("取消添加",{icon:1})
            });
    }
}


VpnExclusiveLineDlg.updateLine = function() {
    var serverId = $("#serverId").val();
    var userNames = $("#username").val();

    var confirmIndex = layer.confirm("确定修改专属线路信息?",
        {btn:['确认修改','取消修改']},
        function () {
            layer.closeAll()
            var loadIndex = layer.load(0,{
                shade:false
            });

            var ajax = new $ax(Feng.ctxPath + "/vpnExclusive/update", function(data){
                Feng.success("修改成功!");
                window.parent.VpnExclusiveLine.refresh();
                VpnExclusiveLineDlg.close();
                layer.closeAll();
            },function(data){
                Feng.error(data.responseJSON.message + "!");
                layer.closeAll();
            });
            ajax.async = false;
            ajax.set("serverId",serverId);
            ajax.set("userNames",userNames);
            ajax.start();
        },function () {
            layer.msg("取消添加",{icon:1})
        });
}

VpnExclusiveLineDlg.selectVpnLit = function(){
    var ajax = new $ax(Feng.ctxPath + "/vpnExclusive/serverList", function (data) {

        var items = data;
        items.forEach(function (item) {
            $("#serverId").append("'<option value='"+item.serverId+"'>"+item.serverIp+"</option>'");
        }) ;

    }, function (data) {
        Feng.error("专属服务器列表获取失败" + data.responseJSON.message + "!");
    });
    ajax.start();
};

$(function() {
    VpnExclusiveLineDlg.selectVpnLit();
});
